mobileFX WebSocketX
In This Topic
    Getting Started with C#
    In This Topic

    Start Visual Studio .NET and create a new C# Windows Forms Application.

     Right-click on Project References and select Add Reference...

    Select COM tree node and type WebSocketX on search textbox; the list will display mobileFX WebSocketX. Check the checkbox next to it and click OK.

    The WebSocketX will appear under References as illustrated below.

    In order to test WebSocketX, you will need an Echo Server.  Creation of the Echo Server is out of scope of this tutorial, but you can easily find instructions on how to build your own or use an online echo server.

    For the purpose of this tutorial, create a simple form with the following controls:

    Control Name Control Type Text

    Click Handler

    Description
    btnConnect Button Connect btnConnect_Click Connect to Echo Server
    btnDisconnect Button Disconnect btnDisconnect_Click Disconnect
    btnSendMessage Button Send Message btnSendMessage_Click Send a message to server
    txtMessage TextBox A textbox to write the message to send
    lblOutput Label

    label1

    Present custom messages.

     

     

      

    Our form, looks like the image below:

     

    Add the following code on your form's code editor:

     

    C#
    Copy Code
    using System;
    using System.Windows.Forms;
    using WebSocketX;
    namespace WebSocketX_Simple
    {
        public partial class Form1 : Form
        {
            WebSocketX.WebSocket wsX;
            public Form1()
            {
                InitializeComponent();
                // Create WebSocketX instance
                wsX = new WebSocketX.WebSocket();
            }
            private void btnConnect_Click(object sender, EventArgs e)
            {
                txtOutput.Text = "Connecting...";
                // Connect to WebSocket Echo Server in insecure mode
                wsX.Open(txtServerURL.Text);
                // Register event handlers
                wsX.OnOpen += WebSocketX_OnOpen;
                wsX.OnMessage += WebSocketX_OnMessage;
                wsX.OnError += WebSocketX_OnError;
                wsX.OnClose += WebSocketX_OnClose;
                btnDisconnect.Enabled = true;
            }
            private void btnDisconnect_Click(object sender, EventArgs e)
            {
                if (wsX.State != WEBSOCKET_STATE.WS_OPEN) { return; }
               
                // Update the UI
                txtOutput.Text = "Disconnecting...";
                btnConnect.Enabled = true;
                btnDisconnect.Enabled = false;
                // Disconnect from server and transmit closing frame
                wsX.Close();
            }
            private void btnSendMessage_Click(object sender, EventArgs e)
            {
                if (String.IsNullOrWhiteSpace(txtMessage.Text))
                {
                    MessageBox.Show("Please enter a message to send to the Echo server.", "Missing message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                if (wsX.State != WEBSOCKET_STATE.WS_OPEN)
                {
                    MessageBox.Show("Please, connect to WebSocket echo server.", "Connection Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                wsX.Send(txtMessage.Text);
                txtMessage.Text = "";
            }
            private void WebSocketX_OnOpen()
            {
                txtOutput.Text = "Connected...";
            }
            private void WebSocketX_OnMessage(string data)
            {
                txtOutput.Text += "\r\n" + data;
            }
            private void WebSocketX_OnError(int ErrorCode, string ErrorDescription)
            {
                txtOutput.Text += "\r\nERROR: (" + ErrorCode.ToString() + ") " + ErrorDescription;
            }
            private void WebSocketX_OnClose()
            {
                txtOutput.Text += "\r\nDisconnected.";
            }
            private void OnTextChanged(object sender, EventArgs e)
            {
                // auto scroll text
                txtOutput.SelectionStart = txtOutput.Text.Length;
                txtOutput.ScrollToCaret();
            }
        }
    }

     NOTE: Do not forget to associate the above mentioned Click Handlers with the corresponding controls.

    Start your server, if it is not already running.  Compile and run!